<template>
	<view>
		<uv-navbar leftText="返回" title="便签" @leftClick="backClick"></uv-navbar>
		
		<view class="note_class">
			<uv-textarea v-model="context" count :maxlength="-1" autoHeight border="bottom" height="150" placeholder="请输入内容"></uv-textarea>
			<view v-if="context" class="note_button_class">
				<uv-button :hairline="false" text="保存" color="#2c9c3c" customTextStyle="color: #ffffff;font-size: 28rpx" @click="noteSubmit()"></uv-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				context: ''
			}
		},
		created() {
			this.userinfo = this.$store.state.userinfo
			this.get_visit_note()
		},
		methods: {
			backClick() {
				uni.navigateBack()
			},
			get_visit_note() {
				var _this = this
				uni.showLoading()
				console.log('查询', _this.userinfo)
				if (_this.userinfo.visit_note !== null && _this.userinfo.visit_note !== undefined) {  
				     _this.context = _this.userinfo.visit_note
					 setTimeout(function () { uni.hideLoading() }, 500);
				} else {
					_this.$api.visit_note({
						userId: _this.userinfo.userId,
						context: _this.context,
						request: 'GET'
					}).then((res) => {
						_this.context = res.data
						setTimeout(function () { uni.hideLoading() }, 500);
					})
				}
			},
			noteSubmit() {
				var _this = this
				uni.showLoading()
				_this.$api.visit_note({
					userId: _this.userinfo.userId,
					context: _this.context,
					request: 'POST'
				}).then((res) => {
					setTimeout(function () {
						_this.userinfo.visit_note = _this.context
						_this.$store.dispatch('set_userinfo', _this.userinfo)
						uni.showToast({
							title: '保存成功',
							icon: 'success'
						})
						_this.backClick()
					}, 500);
				})
			}
		}
	}
</script>

<style>
	.note_class {
		margin-top: 70px;
		padding: 20px;
	}
	
	.note_button_class {
		margin-top: 20px;
	}
</style>
